<template>
  <div>
    <div class="options">
      <el-button @click="info.isShow=true" type="primary" round plain>新增</el-button>
    </div>
    <el-table
      :data="cateList"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="id" label="序号" min-width="180">
      </el-table-column>
      <el-table-column prop="catename" label="分类名称" min-width="180">
      </el-table-column>
      <el-table-column prop="img" label="分类图片" min-width="180">
        <template v-slot="props">
          <img v-if="props.row.img!==''" style="height: 80px" :src="$baseUrl + props.row.img" alt="" />
        </template>
      </el-table-column>
      <el-table-column prop="status" label="分类状态" min-width="180">
        <template slot-scope="props">
          <el-button
            type="success"
            size="mini"
            round
            plain
            v-if="props.row.status === 1"
            >正常</el-button
          >
          <el-button type="danger" size="mini" round plain v-else
            >禁用</el-button
          >
        </template>
      </el-table-column>
      <el-table-column label="操作" min-width="180">
        <template v-slot="props">
          <el-button type="success" @click="edit(props.row)" size="mini" round plain>编辑</el-button>
        <el-button type="danger" @click="del(props.row.id)" size="mini" round plain>删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 调用对话框组件 -->
    <Dialog ref="dialog" :info="info"/>
  </div>
</template>

<script>
// 导入子组件
import Dialog from './Dialog'
// 导入vuex辅助函数
import { mapState, mapActions } from "vuex";
export default {
  data(){
    return {
      info:{
        // 控制对话框组件显示状态
        isShow:false
      }
    }
  },
  components:{Dialog},
  computed: {
    ...mapState("cate", ["cateList"]),
  },
  methods: {
    ...mapActions("cate", ["getCateListAction"]),
    // 删除分类
    del(id){
      this.$myConfirm(()=>{
        this.$axios.post('/catedelete',{id}).then(res=>{
          if(res.code===200){
            this.$message.success(res.msg);
            // 更新分类列表
            this.getCateListAction();
          }else{
            this.$messag.error(res.msg);
          }
        });
      })
    },
    // 编辑按钮点击事件处理函数
    edit(row){
      // 1-显示对话框组件
      this.info.isShow=true;
      // 2-将待编辑的分类信息传递给对话框组件
      this.$refs.dialog.setFormData(row);
    }
  },
  created() {
    this.getCateListAction();
  },
};
</script>

<style>
</style>